import json
import os
import subprocess
import sys

folder_name = "/home/zxhtom/shell/com/yapai/enviroment/"
for root, ds, fs in os.walk(folder_name):
    for file in fs:
        with open(os.path.join(root, file), "r") as file_content:
            fj = json.load(file_content)
            ip = fj["ip"]
            username = fj["user_name"]
            password = fj["password"]
            port = 22
            if "port" in fj:
                port = fj["port"]

            command = "sh {}/nopass.sh -h{} -u{} -p{}".format(
                os.path.dirname(os.path.abspath(sys.argv[0])), ip, username, password
            )
            print(command)
            result = subprocess.getoutput(
                "sh {}/nopass.sh -h{} -u{} -p{} 2>/dev/null".format(
                    os.path.dirname(os.path.abspath(sys.argv[0])),
                    ip,
                    username,
                    password,
                )
            )
            print(result)
            subprocess.getoutput(
                "sh {}/sshconfig.sh -h{} -u{} -p{}".format(
                    os.path.dirname(os.path.abspath(sys.argv[0])),
                    ip,
                    username,
                    port,
                )
            )
            with open("host", "a") as f:
                f.write("{}@{}:{}\n".format(username, ip, port))
